package yja.gzw.app.view


import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View

class CoordinateAxisView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val paint = Paint()
    private var maxYValue = 50f // y 轴最大值，可修改
    private val scaleCountY = 5 // y 轴刻度数量
    private var scaleIntervalValueY = maxYValue / scaleCountY // y 轴每个刻度代表的值
    private var maxXValue = 7f // x 轴最大值，可修改
    private var scaleCountX = 7 // x 轴刻度数量，可修改
    private var scaleIntervalValueX = maxXValue / scaleCountX // x 轴每个刻度代表的值
    private val lineColor = Color.parseColor("#F6F6F6")
    private var rectHeights = FloatArray(scaleCountX) { 100f } // 存储每个矩形的高度，初始值为 100f

    // 设置指定索引矩形的高度
    fun setRectHeight(index: Int, height: Float) {
        if (index in 0 until scaleCountX) {
            rectHeights[index] = height
            invalidate() // 重绘视图
        }
    }

    // 设置 y 轴最大值
    fun setMaxYValue(value: Float) {
        this.maxYValue = value
        this.scaleIntervalValueY = maxYValue / scaleCountY
        invalidate() // 重绘视图
    }

    // 设置 x 轴刻度数量
    fun setScaleCountX(count: Int) {
        if (count > 0) {
            this.scaleCountX = count
            this.scaleIntervalValueX = maxXValue / scaleCountX
            this.rectHeights = FloatArray(scaleCountX) { 100f } // 重置矩形高度数组
            invalidate() // 重绘视图
        }
    }

    // 设置 x 轴最大值
    fun setMaxXValue(value: Float) {
        if (value > 0) {
            this.maxXValue = value
            this.scaleIntervalValueX = maxXValue / scaleCountX
            invalidate() // 重绘视图
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        // 设置画笔属性
        paint.color = Color.BLACK
        paint.strokeWidth = 5f
        paint.textSize = 30f

        // 获取视图的宽度和高度
        val width = width.toFloat()
        val height = height.toFloat()

        // 原点位置，这里将原点设置在左下角
        val originX = 50f
        val originY = height - 50f

        // 绘制 x 轴
        canvas.drawLine(originX, originY, width - 50f, originY, paint)
        // 绘制 x 轴箭头
        canvas.drawLine(width - 50f, originY, width - 60f, originY - 10f, paint)
        // 绘制 x 轴标签
        canvas.drawText("X", width - 30f, originY - 20f, paint)

        // 设置画笔颜色为白色以绘制 y 轴
        paint.color = Color.WHITE
        // 绘制 y 轴
        canvas.drawLine(originX, originY, originX, 50f, paint)
        // 绘制 y 轴箭头
        canvas.drawLine(originX, 50f, originX - 10f, 60f, paint)
        // 绘制 y 轴标签
        canvas.drawText("Y", originX + 20f, 30f, paint)

        // 绘制原点
        paint.color = Color.RED
        canvas.drawCircle(originX, originY, 10f, paint)

        // 绘制 y 轴数字和与 x 轴等长的线条，设置线条颜色
        paint.color = lineColor
        val scaleIntervalY = (originY - 50f) / scaleCountY
        for (i in 1..scaleCountY) {
            val currentY = originY - i * scaleIntervalY
            // 绘制与 x 轴等长的线条
            canvas.drawLine(originX, currentY, width - 50f, currentY, paint)
            // 绘制刻度对应的数字
            val currentValue = i * scaleIntervalValueY
            paint.color = Color.BLACK
            canvas.drawText(currentValue.toString(), originX - 30f, currentY + 10f, paint)
            paint.color = lineColor
        }

        // 绘制 x 轴刻度、数字和蓝色矩形
        val scaleIntervalX = (width - 100f) / scaleCountX
        val rectWidth = scaleIntervalX * 0.8 // 矩形宽度为刻度间隔的 0.8 倍
        for (i in 0 until scaleCountX) {
            val currentX = originX + i * scaleIntervalX
            val nextX = currentX + scaleIntervalX
            val midX = (currentX + nextX) / 2
            paint.color = lineColor
            // 绘制刻度线朝下
            canvas.drawLine(currentX, originY, currentX, originY + 10f, paint)

            val currentValue = (i + 1) * scaleIntervalValueX
            paint.color = Color.BLACK
            val textWidth = paint.measureText(currentValue.toString())
            canvas.drawText(
                currentValue.toString(),
                midX - textWidth / 2,
                originY + 30f,
                paint
            )
            paint.color = lineColor

            // 绘制蓝色矩形
            paint.color = Color.BLUE
            val rectLeft = midX - rectWidth / 2
            val rectTop = originY - rectHeights[i]
            canvas.drawRect(rectLeft.toFloat(), rectTop,
                (rectLeft + rectWidth).toFloat(), originY, paint)
        }
    }
}